#include "scanner.h"

// to build this with all linkage, I found the following to be idiotproof:
//   gcc driver.c scanner.c parser.c vm.c -o plc
//   ./pcc input.txt output.txt

int main(int argc, const char **argv){
  // args in order:
  // 0: PL0 code input filename
  // 1: output filename

  char inputFileName[MAX_PROGRAM_LENGTH], outputFileName[MAX_PROGRAM_LENGTH];
  strcpy(inputFileName, argv[1]);
  FILE* inPtr = fopen(inputFileName, "r");
  strcpy(outputFileName, argv[2]);
  FILE* outPtr = fopen(outputFileName, "w");

  if(inPtr == NULL){
    printf("File not found: %s", inputFileName);
    return 0;
  }
  if(outPtr == NULL){
    printf("File not found: %s", outputFileName);
    return 0;
  }

  // scan in PL0 code, get lexeme list
  char lexList[MAX_PROGRAM_LENGTH];
  scan_file(inPtr, outPtr, lexList);
  printf("SUP WORLD %s", lexList);

  // parse lexeme list (validate code, get symbol table)
  instruction assemblyCode[MAX_CODE_LENGTH];
  int codeLength = parseLexemeList(outPtr, lexList, assemblyCode);

  // generate assembly for vm (potentially done in parser)

  // run assembly on vm, print output
  runVM(outPtr, assemblyCode, codeLength);


  return 0;
}
